import { _decorator, Component, Node, Prefab, randomRangeInt, SpriteFrame, SpriteRenderer, Vec3 } from 'cc';
import { Item } from './Items/Item';
import { PoolManager } from '../../Scripts/Framework/Managers/PoolManager';
import { Ammo } from './Items/Ammo';
import { Module } from './Items/Module';
import { Gun } from './Items/Gun';
import { Melee } from './Items/Melee';
import { Heal } from './Items/Heal';
import { Armor } from './Items/Armor';
import { Pack } from './Items/Pack';
import { InGameManager } from './Managers/InGameManager';
import { Adorn } from './Items/Adorn';
import { GameDataManager } from '../../MainPack/Scripts/GameDataManager';
const { ccclass, property } = _decorator;

@ccclass('ItemBox')
export class ItemBox extends Component {

    @property([SpriteFrame])
    typeSfs: SpriteFrame[] = [];

    spRenderer: SpriteRenderer;
    items: Item[] = [];

    static Init(pos: Vec3): ItemBox;
    static Init(parent: Node, isRandom: boolean): ItemBox;
    static Init(arg: Vec3 | Node, isRandom = false): ItemBox {
        let box = PoolManager.GetNodeByPrefab(InGameManager.Instance.itemBox, InGameManager.Instance.boxs) as Node;
        if (arg instanceof Vec3) box.setWorldPosition(arg);
        else if (arg instanceof Node) box.setWorldPosition(arg.getWorldPosition());
        if (isRandom) {
            //开场随机战利品
            for (let i = 0; i < 3; i++) {
                let x = randomRangeInt(0, 8);
                let item: Item;
                switch (x) {
                    case 0:
                        item = Gun.Init(randomRangeInt(0, 5), randomRangeInt(0, 3));
                        break;
                    case 1:
                        item = Module.Init(randomRangeInt(0, 5), randomRangeInt(0, 3));
                        break;
                    case 2:
                        item = Ammo.Init(randomRangeInt(0, 5));
                        break;
                    case 3:
                        item = Melee.Init(randomRangeInt(0, 3));
                        break;
                    case 4:
                        item = Heal.Init(randomRangeInt(0, 5), randomRangeInt(1, 6));
                        break;
                    case 5:
                        item = Armor.Init(randomRangeInt(0, 6));
                        break;
                    case 6:
                        item = Pack.Init(randomRangeInt(0, 3));
                        break;
                    case 7:
                        let storage = GameDataManager.Instance.getArrayData<number[]>("AllSkin");
                        let randoms: number[][] = [];
                        for (let i = 0; i < storage.length; i++) {
                            const element = storage[i];
                            for (let j = 0; j < element.length; j++) {
                                const element2 = element[j];
                                if (element2 == 0) randoms.push([i, j]);
                            }
                        }
                        if (randoms.length == 0) {
                            item = Pack.Init(randomRangeInt(0, 3));
                            break;
                        }
                        let data = randoms[randomRangeInt(0, randoms.length)];
                        item = Adorn.Init(data[0], data[1]);
                        break;
                }
                box.getComponent(ItemBox).items.push(item);
            }
        }
        box.getComponent(ItemBox).CheckSp();
        return box.getComponent(ItemBox);
    }

    protected onLoad(): void {
        // Events.once(EventName.JsonCompleted, this.TestInit, this);
    }

    protected update(dt: number): void {
        this.RotateSp();
    }

    TestInit() {
        // this.items.push(Ammo.Init(0), Ammo.Init(1), Ammo.Init(2), Ammo.Init(3), Ammo.Init(4), Module.Init(0, 0), Module.Init(1, 0), Module.Init(2, 0), Module.Init(3, 0), Module.Init(4, 0))
    }

    RotateSp() {
        this.spRenderer.node.lookAt(InGameManager.Instance.cameraNode.getWorldPosition());
    }

    CheckSp() {
        if (!this.spRenderer) this.spRenderer = this.node.children[2].getComponent(SpriteRenderer);
        if (this.items.length == 0) return this.spRenderer.spriteFrame = null;
        let num = -1;
        if (this.items.find((value, index, obj) => { if (value instanceof Gun || value instanceof Melee) return value; })) num = 0;
        else if (this.items.find((value, index, obj) => { if (value instanceof Armor) return value; })) num = 1;
        else if (this.items.find((value, index, obj) => { if (value instanceof Pack) return value; })) num = 2;
        else if (this.items.find((value, index, obj) => { if (value instanceof Module) return value; })) num = 3;
        else if (this.items.find((value, index, obj) => { if (value instanceof Ammo) return value; })) num = 4;
        else if (this.items.find((value, index, obj) => { if (value instanceof Heal) return value; })) num = 5;
        if (num == -1) this.spRenderer.spriteFrame = null;
        else this.spRenderer.spriteFrame = this.typeSfs[num];
    }

}


